package com.nineclock.message.service.impl;

import com.nineclock.message.jpush.JPushManager;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.MessageService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private JPushManager jPushManager;
    /**
     * 推送消息
     * @param message
     */
    @Override
    public void pushMessage(NcMessage message) throws Exception {
        // 消息保存到mongoDB
        message.setCreateTime(new Date());
        mongoTemplate.save(message);
        // 调用极光推送API
        jPushManager.sendNotificationByAlias(message.getTitle(), message.getContent(), message.getTargets(), null);

    }
}
